//
//  ViewController.m
//  uici
//
//  Created by MB.Lee on 17/1/10.
//  Copyright © 2017年 com.coubi.ii. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIView *indicatorView;
@property (nonatomic, strong) UIButton *button;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    [self setUpTitleView];
    
    
}
-(void)setUpTitleView{
    UIView *titleView = [[UIView alloc]init];
    titleView.width = self.view.width;
    titleView.height = 40;
    titleView.y = 64;
    [self.view addSubview:titleView];
    titleView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    
    UIView *indicator = [[UIView alloc]init];
    indicator.height = 4;
    indicator.y = titleView.height - indicator.height;
    indicator.backgroundColor =[UIColor redColor];
    [titleView addSubview:indicator];
    self.indicatorView = indicator;
    
    NSArray *titleArr = @[@"全部",@"视频",@"音频",@"图片",@"段子"];
    for (int i = 0; i < titleArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:titleArr[i] forState:0];
        [button setTitleColor:[UIColor redColor] forState:0];
        [button setTitleColor:[UIColor greenColor] forState:UIControlStateDisabled];
        button.width = titleView.width * 0.2;
        button.height = titleView.height - indicator.height;
        button.x = button.width * i;
        [titleView addSubview:button];
        //强制布局（强制更新子空间的frame）
        [button layoutIfNeeded];
        
        [button addTarget:self action:@selector(clik:) forControlEvents:UIControlEventTouchUpInside];
        
        if (i == 0 ) {
            button.enabled = NO;
            self.button = button;
            
            //让按钮内部label 根据文字内容来计算尺寸
            [button.titleLabel sizeToFit];
            self.indicatorView.width = button.titleLabel.width;
            self.indicatorView.centerX = button.centerX;
            
        }
        
    }
    
    
    
    
}
-(void)clik:(UIButton *)button{
    self.button.enabled = YES;
    button.enabled  = NO;
    self.button = button;
    [UIView animateWithDuration:0.25 animations:^{
         self.indicatorView.width = button.titleLabel.width;
        self.indicatorView.centerX = button.centerX;
       
    }];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
